package com.example.service_sms.service.impl;

import com.aliyun.tea.TeaException;
import com.example.common.exception.GuliException;
import com.example.service_sms.service.SMSService;
import com.example.service_sms.utils.SMSConst;
import com.example.service_sms.utils.SMSUtils;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @Author:tjf
 * @Description:
 */
@Service
public class SMSServiceImpl implements SMSService {
    @Value("${aliyun.sms.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.sms.accessKeySecret}")
    private String accessKeySecret;
    @Resource(name = "stringRedisTemplate")
    private StringRedisTemplate redisTemplate;
    /**
     * 发送登录短信
     * @param phone
     * @return
     */
    @Override
    public int sendLoginMessage(String phone) {
        // 判断该手机号是否有短信处于有效期
        Boolean exist = redisTemplate.hasKey(SMSConst.SMS_LOGIN_KEY + phone);
        if (exist == true){
            return SMSConst.SMS_NOT_EXPIRE;
        }
        // 发送登录短信
        String code = RandomStringUtils.randomNumeric(4);
        boolean send = sendMessage(accessKeyId, accessKeySecret, phone, code);
        if (send == false){
            return SMSConst.SMS_FAIL;
        }
        // 设置短信过期时间
        redisTemplate.opsForValue().set(SMSConst.SMS_LOGIN_KEY + phone, code,
                                    5, TimeUnit.MINUTES);
        return SMSConst.SMS_SUCCESS;
    }

    /**
     * 发送注册短信
     * @param phone
     * @return
     */
    @Override
    public int sendRegisterMessage(String phone) {
        // 判断该手机号是否存在未过期的验证码
        Boolean exist = redisTemplate.hasKey(SMSConst.SMS_REGISTER_KEY + phone);
        if (exist == true){
            return SMSConst.SMS_NOT_EXPIRE;
        }
        // 发送登录短信
        String code = RandomStringUtils.randomNumeric(4);
        boolean send = sendMessage(accessKeyId, accessKeySecret, phone, code);
        if (send == false){
            return SMSConst.SMS_FAIL;
        }
        // 设置短信过期时间
        redisTemplate.opsForValue().set(SMSConst.SMS_REGISTER_KEY + phone, code,
                5, TimeUnit.MINUTES);
        return SMSConst.SMS_SUCCESS;
    }

    /**
     *
     * @param accessKeyId
     * @param accessKeySecret
     * @param phone 手机号
     * @param code 验证码
     * @return
     */
    private static boolean sendMessage(String accessKeyId, String accessKeySecret, String phone, String code){
//        try {
//            com.aliyun.dysmsapi20170525.Client client = SMSUtils.createClient(accessKeyId, accessKeySecret);
//            com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
//                    .setSignName("阿里云短信测试")
//                    .setTemplateCode("SMS_154950909")
//                    .setPhoneNumbers(phone)
//                    .setTemplateParam("{\"code\":" + code + "}");
//            com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
//            // 复制代码运行请自行打印 API 的返回值
//            client.sendSmsWithOptions(sendSmsRequest, runtime);
//        } catch (Exception error) {
//            error.printStackTrace();
//            return false;
//        }
        System.out.println("code = " + code);
        return true;
    }
}
